{extend name="base" /}
{block name="body"}
<div class="layui-body">
    <!--tab标签-->
    <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title">
            <li class=""><a href="{:url('admin/UsedTruck/index')}">信息管理</a></li>
            <li class=""><a href="{:url('admin/UsedTruck/add')}">添加信息</a></li>
            <li class="layui-this">编辑信息</li>
            <li class=""><a href="{:url('admin/UsedTruck/reading',['id'=>$usedtruck['id']])}">查看信息</a></li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form class="layui-form form-container" action="{:url('admin/UsedTruck/update')}" method="post">
                    <div class="layui-form-item">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="{$usedtruck.title}" required  lay-verify="required" placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车型</label>
                        <div class="layui-input-block">
                            <select name="pid[]" class="site">
                                <option value="0">车型</option>
                                {foreach name="used_list" item="vo"}
                                {if $vo.pid == 0 and $vo.cid == 1}
                                {foreach name='used_list' item='lo'}
                                {if $lo.pid[0] == $vo.id}
                                <option value="{$lo.id}" {if $usedtruck.type == $lo.id}selected="selected"{/if}>{$lo.name}</option>
                                {/if}
                                {/foreach}
                                {/if}
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">品牌</label>
                        <div class="layui-input-block">
                            <select name="pid[]" class="site">
                                <option value="{$usedtruck.brand}">{$classify_list[$usedtruck['brand']]}</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车系</label>
                        <div class="layui-input-block">
                            <select name="pid[]" class="site">
                                <option value="{$usedtruck.series}">{$classify_list[$usedtruck['series']]}</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">型号</label>
                        <div class="layui-input-block">
                            <select name="pid[]">
                                <option value="{$usedtruck.version}">{$classify_list[$usedtruck['version']]}</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆所在地</label>
                        <div class="layui-input-inline">
                            <select name="site[]" lay-verify="required" id="site">
                                <option value="0">全国</option>
                                {foreach name="area_list" item="vo"}
                                {if $vo.parent_id == 0}
                                <option value="{$vo.id}" {if $usedtruck.site[0] == $vo.id}selected="selected"{/if}>{$vo.name}</option>
                                {/if}
                                {/foreach}
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select name="site[]" lay-verify="required" id="site_son">
                                <option value="0">全省</option>
                                {foreach name="area_list" item="vo"}
                                {if $vo.parent_id == $usedtruck.site[0]}
                                {if $usedtruck.site[1] != 0}
                                <option value="{$vo.id}" {if $usedtruck.site[1] == $vo.id}selected="selected"{/if}>{$vo.name}</option>
                                {/if}
                                {/if}
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">表显里程</label>
                        <div class="layui-input-block">
                            <input type="text" name="mileage" value="{$usedtruck.mileage}" placeholder="请输入表显里程" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">价格(万)</label>
                        <div class="layui-input-block">
                            <input type="text" name="price" value="{$usedtruck.price}" placeholder="请输入价格" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">降价金额(万)</label>
                        <div class="layui-input-block">
                            <input type="text" name="low_price" value="{$usedtruck.low_price}" placeholder="(选填)请输入降价金额" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">购买方式</label>
                        <div class="layui-input-block">
                            <input type="radio" name="methodtype" value="1" title="全款" {if $usedtruck.methodtype == 1}checked="checked"{/if}>
                            <input type="radio" name="methodtype" value="0" title="分期" {if $usedtruck.methodtype == 0}checked="checked"{/if}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">上牌时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="apply_time" value="{$usedtruck.apply_time}" class="layui-input datetime">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="padding-left: 0;width: 95px;">强险过期时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="past_time" value="{$usedtruck.past_time}" class="layui-input datetime">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">卖家附言</label>
                        <div class="layui-input-block">
                            <textarea name="content" placeholder="（选填）请输入卖家附言" class="layui-textarea" id="content">{$usedtruck.content}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图集</label>
                        <div class="layui-input-block">
                            <button type="button" id="upload-photo-btn" class="layui-btn">上传图集</button>
                            <div id="photo-container">
                                {if condition="!empty($usedtruck['photo'])"}
                                {foreach name="$usedtruck['photo']" item="vo"}
                                <div class="photo-list">
                                    <input type="text" name="photo[]" value="{$vo}" class="layui-input layui-input-inline">
                                    <button type="button" class="layui-btn layui-btn-danger remove-photo-btn">移除</button>
                                </div>
                                {/foreach}
                                {/if}
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">查看次数</label>
                        <div class="layui-input-block">
                            <input type="text" name="reading" value="{$usedtruck.reading}" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">置顶</label>
                        <div class="layui-input-block">
                            <input type="radio" name="is_top" value="1" title="置顶" {if $usedtruck.is_top == 1}checked="checked"{/if}>
                            <input type="radio" name="is_top" value="0" title="未置顶" {if $usedtruck.is_top == 0}checked="checked"{/if}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="radio" name="status" value="1" title="已审核" {if $usedtruck.status == 1}checked="checked"{/if}>
                            <input type="radio" name="status" value="0" title="未审核" {if $usedtruck.status == 0}checked="checked"{/if}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">推荐</label>
                        <div class="layui-input-block">
                            <input type="radio" name="is_recommend" value="1" title="推荐" {if $usedtruck.is_recommend == 1}checked="checked"{/if}>
                            <input type="radio" name="is_recommend" value="0" title="未推荐" {if $usedtruck.is_recommend == 0}checked="checked"{/if}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="{$usedtruck.id}">
                            <button class="layui-btn" lay-submit lay-filter="*">更新</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<script src="__JS__/kindeditor/kindeditor.config.js"></script>
<script src="__JS__/kindeditor/kindeditor-all-min.js"></script>
{/block}
{block name="script"}
<script>
    $(document).ready(function () {
        var _kindEditor;
        KindEditor.ready(function(K) {
            _kindEditor = K.create('#content', KindEditorOptions);

            K('#upload-photo-btn').click(function () {
                var photo_list_item = '';
                _kindEditor.loadPlugin('multiimage', function() {
                    _kindEditor.plugin.multiImageDialog({
                        showRemote : false,
                        imageUrl : K('#photo').val(),
                        clickFn : function(data) {
                            $.each(data, function (index, item) {
                                photo_list_item += '<div class="photo-list"><input type="text" name="photo[]" value="' + item.url + '" class="layui-input layui-input-inline">';
                                photo_list_item += '<button type="button" class="layui-btn layui-btn-danger remove-photo-btn">移除</button></div>'
                            });
                            $('#photo-container').append(photo_list_item);
                            _kindEditor.hideDialog();
                        }
                    });
                });
            });
        });

        $('#photo-container').on('click', '.remove-photo-btn', function () {
            $(this).parent('.photo-list').remove();
        });


        // $('#site').next().css({'float':'left','width':'40%'});
        // $('#site_son').next().css({'float':'left','width':'60%'});

        form.on('select',function(data){
            $("select#site").next().find('dl').on('click','dd',function(){
                var parent_id = $(this).attr('lay-value');
                // console.log('开始');
                $.ajax({
                    url:'/index/area',
                    data:"id="+parent_id,
                    success:function(ell){
                        var i = 0;
                        $('#site_son').html('');
                        $('#site_son').append('<option value="0">全省</option>');
                        $('#site_son').next().find('dl').html('');
                        $('#site_son').next().find('dl').append('<dd lay-value="0" class="layui-this">全省</dd>');
                        $('#site_son').next().find('input').val('全省');
                        for(;i<ell.data.length;i++){
                            $('#site_son').append('<option value='+ell.data[i].id+'>'+ell.data[i].name+'</option>');
                            $('#site_son').next().find('dl').append('<dd lay-value='+ell.data[i].name+'>'+ell.data[i].name+'</dd>');
                        }
                        form.render('select');
                        // $('#site').next().css({'float':'left','width':'40%'});
                        // $('#site_son').next().css({'float':'left','width':'60%'});
                        // console.log('完毕');
                    }
                })
            });

            //车型等4级联动
            $("select.site").next().find('dl').on('click','dd',function(){
                var jilu = '.layui-form-item';
                var parent_id = $(this).attr('lay-value');
                var index = $(this).parents(jilu).index()+1;

                if(parent_id != 0){
                    $.ajax({
                        url:'/admin/used_classify2/jilm',
                        data:"id="+parent_id,
                        success:function(ell){
                            var i = 0;
                            xuan();

                            for(;i<ell.data.length;i++){
                                $(jilu).eq(index).find('select').append('<option value='+ell.data[i].id+'>'+ell.data[i].name+'</option>');
                                $(jilu).eq(index).find('dl').append('<dd lay-value='+ell.data[i].name+'>'+ell.data[i].name+'</dd>');
                            }
                            form.render('select');

                        }
                    });
                }else{
                    xuan();
                    form.render('select');
                }

                function xuan(){
                    // console.log(index);
                    if(index != 0){
                        for(var j=1;j<5;j++){
                            if(j > (index-1)){
                                // console.log('j:'+j);
                                $(jilu).eq(j).find('select').html('');
                                $(jilu).eq(j).find('dl').html('');
                                $(jilu).eq(j).find('select').append('<option value="0">全部</option>');
                                $(jilu).eq(j).find('dl').append('<dd lay-value="0">全部</dd>');

                            }
                        }
                    }
                }

            });

        });

    });
</script>
{/block}